const app = getApp()
import {
  imageUrl
} from "../../utils/imageutil.js"
Page({
  data: {
    imageUrl,
    userInfo: wx.getStorageSync('userInfo'),
    distinguish: '', //顶部tap数据参数1住家3服务
    typescurr: -1, //顶部tab切换状态
    list2_index: -1, //顶部class
    types: [{ //顶部分类
      title: '全部服务',
      id: '1'
    }, {
      title: '服务内容',
      id: '2'
    }, {
      title: '筛选',
      id: '3'
    }],
    _index: 0, //排序class
    sort: [{
        title: '离我最近',
        by: 4,
        desc: 1
      },
      {
        title: '星级排序',
        by: '6',
        desc: 1
      },
      // {
      //   title:'好评优先',
      //   by:2
      // },
      // {
      //   title: '薪资排序',
      //   by: 5,
      //   desc: 1
      // },
       {
        title: '综合排序',
        by: '',
        desc: 1
      }
    ],
    housekeeperListPage: 1,
    housekeeperListby: 4,
    desc: 1,
    housekeeperListFlag: true,
    housekeeperList: [],
    calendarHeight: 0,
    calendarHeightAndBtnH: 0,
    whether: [], //是否住家
    serve: [], //服务内容
    home: [],
    services: []
  },
  onLoad: function (options) {
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
    })
    this.getWorkType(1, 'whether').then(() => {
      if (options.name) {
        this.data.whether.forEach((v, i) => {
          if (v.name == options.name) {
            let value = 'whether[' + i + '].check'
            let values = this.data.whether[i].name
            this.setData({
              [value]: !this.data.whether[i].check,
              services: values
            })
          }
        })
      }
      this.gethousekeeperList();
    })
    this.getWorkType(3, 'serve')
    this.setData({
      calendarHeight: app.globalData.windowHeight - ((80 + 132) / 750 * app.globalData.windowWidth),
      calendarHeightAndBtnH: app.globalData.windowHeight - (80 / 750 * app.globalData.windowWidth),
    })
  },
  getWorkType(distinguish, val) {
    return new Promise((reslove, reject) => {
      app.globalData.post({
        data: {
          do: 'WorkType',
          distinguish
        }
      }).then(res => {
        this.setData({
          [val]: res.data.data
        })
        reslove()
      })
    })
  },
  checkage(e) { //单选框
    this.removeprovinces()
    let value = 'whether[' + e.currentTarget.dataset.index + '].check'
    let values = this.data.whether[e.currentTarget.dataset.index].name
    this.setData({
      [value]: !this.data.whether[e.currentTarget.dataset.index].check,
      services: values
    })
  },
  checkboxChange(e) { //多选框
    let string = "serve[" + e.target.dataset.index + "].selected"
    this.setData({
      [string]: !this.data.serve[e.target.dataset.index].selected,
    })
    let detailValue = this.data.serve.filter(it => it.selected).map(it => it.name)
    console.log('所有选中的值为：', detailValue)
    this.setData({
      home: detailValue
    })
  },
  getall() { //确定按钮
    console.log('确定')
    this.setData({
      housekeeperListPage: 1,
      housekeeperListby: 1,
      housekeeperListFlag: true,
      housekeeperList: []
    })
    this.gethousekeeperList()
  },
  removeprovinces() { //清空是否住家
    this.data.whether.forEach((v, i) => {
      let value = 'whether[' + i + '].check';
      this.setData({
        [value]: false,
      })
    })
    this.setData({
      home: ''
    })
  },
  remove() { //清空服务内容
    this.data.serve.forEach((v, i) => {
      let string = 'serve[' + i + '].selected';
      this.setData({
        [string]: false,
      })
    })
    this.setData({
      services: ''
    })
  },
  removeall() { //清空按钮
    console.log('清空')
    this.removeprovinces();
    this.remove();
    this.setData({
      typescurr: -1
    })
  },
  onReachBottom() {
    this.gethousekeeperList()
  },
  gethousekeeperList() { //家政员列表
    app.globalData.post({
      data: {
        do: 'housekeeperList',
        uid: wx.getStorageSync('userInfo').id,
        page: this.data.housekeeperListPage,
        by: this.data.housekeeperListby,
        desc: this.data.desc,
        pagesize: 10,
        services: this.data.services, //是否住家
        home: this.data.home //服务内容
      }
    }).then(res => {
      wx.stopPullDownRefresh();
      this.setData({
        typescurr: -1
      })
      if (res.data.data.length == 0) {
        this.setData({
          housekeeperListFlag: false
        })
      } else {
        let arr = this.data.housekeeperList.concat(res.data.data);
        this.setData({
          housekeeperList: arr,
          housekeeperListPage: this.data.housekeeperListPage + 1
        })
      }
    })
  },
  changetypescurr(e) { //顶部tao切换
    this.setData({
      typescurr: this.data.typescurr == e.currentTarget.dataset.id ? -1 : e.currentTarget.dataset.id,
      list2_index: e.currentTarget.dataset.index,
    })
  },
  sortchange(e) { //排序切换
    let by = e.currentTarget.dataset.by;
    let desc = this.data.sort[e.currentTarget.dataset.index].desc
    if (this.data.housekeeperListby == by) {
      desc = desc == 1 ? 2 : 1
    }
    this.setData({
      _index: e.currentTarget.dataset.index,
      housekeeperListby: by,
      housekeeperListPage: 1,
      housekeeperList: [],
      desc: desc,
      ['sort[' + e.currentTarget.dataset.index + '].desc']: desc
    })
    this.gethousekeeperList()
  },
  changebtn() { //候选
    wx.navigateTo({
      url: './candidate?type=1',
    })
  },
  to_aunt(e) { //家政员详情
    wx.navigateTo({
      url: '../aunt/index?uid=' + e.currentTarget.dataset.id + '&type=1',
    })
  },
  to_store(e) {
    wx.navigateTo({
      url: '../housekeeping/index?store_id=' + e.currentTarget.dataset.store_id,
    })
  },
})